<?php

namespace App\Services;

use App\Http\Controllers\Controller;
use App\Repositories\DirectoryRepository;
use JWTAuth;
use Illuminate\Support\Facades\Log;

class DirectoryService extends Controller
{

    protected $directoryRepository;

    public function __construct(DirectoryRepository $directoryRepository) 
    {
        $this->directoryRepository = $directoryRepository;
    }

    //For POCOR-8104 Start...
    public function getUserTypeList($params)
    {
        try {
            $data = $this->directoryRepository->getUserTypeList($params);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch User Type List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('User Type List Not Found');
        }
    }


    public function getUniqueOpenemisId($params)
    {
        try {
            $data = $this->directoryRepository->getUniqueOpenemisId($params);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to generate Openemis Id.',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Failed to generate Openemis Id.');
        }
    }


    public function getAutoGeneratedPassword($params)
    {
        try {
            $data = $this->directoryRepository->getAutoGeneratedPassword($params);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to generate Password.',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Failed to generate Password.');
        }
    }


    public function getContactTypes($params)
    {
        try {
            $data = $this->directoryRepository->getContactTypes($params);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Contact Type List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Contact Type List Not Found');
        }
    }


    public function getStaffCustomFields($params)
    {
        try {
            $data = $this->directoryRepository->getStaffCustomFields($params);
            $resp = [];

            foreach($data as $k => $d){
                //dd($d);
                $section = $d['section'];
                $arr['staff_custom_form_id'] = $d['staff_custom_form_id'];
                $arr['staff_custom_field_id'] = $d['staff_custom_field_id'];
                $arr['section'] = $d['section'];
                $arr['name'] = $d['name'];
                $arr['is_mandatory'] = $d['is_mandatory'];
                $arr['is_unique'] = $d['is_unique'];
                $arr['order'] = $d['order'];
                $arr['params'] = $d['staff_custom_field']['params']??Null;
                $arr['field_type'] = $d['staff_custom_field']['field_type']??Null;
                //$arr['options'] = $d['studentCustomField']['studentCustomFieldOption']??Null;
                $arr['options'] = $d['staff_custom_field']['staff_custom_field_option']??Null;
                $arr['description'] = $d['staff_custom_field']['description']??Null;


                $resp[$section][] = $arr;
            }

            return $resp;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Staff Custom Fields List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Staff Custom Fields List Not Found');
        }
    }


    public function getFieldOptions($params)
    {
        try {
            $data = $this->directoryRepository->getFieldOptions($params);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Field Options List from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Field Options List Not Found');
        }
    }


    public function getFieldOptionData($params, $fieldOptionId)
    {
        try {
            $data = $this->directoryRepository->getFieldOptionData($params, $fieldOptionId);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Field Option Data from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Field Option Data Not Found');
        }
    }


    public function getUserByIdentityNumber($params, $identityTypeId, $identityNumber)
    {
        try {
            $data = $this->directoryRepository->getUserByIdentityNumber($params, $identityTypeId, $identityNumber);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch User Data from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('User Data Not Found');
        }
    }


    public function getUserByBasicInfo($params)
    {
        try {
            $data = $this->directoryRepository->getUserByBasicInfo($params);

            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch User Data from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('User Data Not Found');
        }
    }


    public function getRelationshipTypes($params)
    {
        try {
            $data = $this->directoryRepository->getRelationshipTypes($params);
            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Relationship Types from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Relationship Types Not Found');
        }
    }


    public function getStaffType($params)
    {
        try {
            $data = $this->directoryRepository->getStaffType($params);

            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Staff Types from DB',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse('Staff Types Not Found');
        }
    }

    //For POCOR-8104 End...


    //For POCOR-8194 Start...
    public function getStaffPositionGrades($params)
    {
        try {
            $data = $this->directoryRepository->getStaffPositionGrades($params);

            return $data;
            
        } catch (\Exception $e) {
            Log::error(
                'Failed to fetch Staff position grades.',
                ['message'=> $e->getMessage(), 'trace' => $e->getTraceAsString()]
            );
            return $this->sendErrorResponse($e->getMessage());
        }
    }
    //For POCOR-8194 End...

}